package com.woniu113.junk.site.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.site.dto.AllFindAllWastePriceDTO;
import com.woniu113.junk.site.dto.FindAllWastePriceDTO;
import com.woniu113.junk.site.dto.WastePriceDTO;
import com.woniu113.junk.site.form.wasteprice.AddWastePriceForm;
import com.woniu113.junk.site.form.wasteprice.FindAllWastePriceForm;
import com.woniu113.junk.site.form.wasteprice.FindWastePriceByTypeAndCityForm;
import com.woniu113.junk.site.model.StationInfo;
import com.woniu113.junk.site.model.WastePrice;
import com.woniu113.junk.site.param.wasteprice.AddWastePriceParam;
import com.woniu113.junk.site.param.wasteprice.FindAllWastePriceParam;
import com.woniu113.junk.site.param.wasteprice.FindWastePriceByTypeAndCityParam;
import com.woniu113.junk.site.service.WastePriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/wastePrice")
@Api(value = "WastePriceController", tags = "废品价格信息接口")
public class WastePriceController {
    @Resource
    private WastePriceService wastePriceService;

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private StationInfoController stationInfoController;

    /**
     * 新增废品价格
     * @param form:新增废品价格信息
     * @param token:当前登录用户token
     * @return 返回新增的价格信息、成功提示和code
     * @throws Exception
     */
    @PostMapping("/addWastePrice")
    @ApiOperation(value = "新增废品价格")
    public Result<WastePrice> addWastePrice(@RequestBody AddWastePriceForm form,@RequestHeader String token) throws Exception {
        System.out.println("价格："+form.getPrice());
        System.out.println("区站点："+form.getStationId());
        System.out.println("类型："+form.getTypeId());
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        //调用新增方法
        WastePrice newWastePrice = wastePriceService.addWastePrice(BeanUtil.toBean(form, AddWastePriceParam.class),adminId);
        return Result.getResult(WastePrice.class).setData(newWastePrice).setMsg("新增废品价格成功").setCode(200);
    }

    /**
     * 更新废品价格信息
     * @param price 更新价格
     * @param priceId 要更新的废品价格id
     * @param token 当前登录信息
     * @return 返回成功信息和code
     * @throws Exception
     */
    @GetMapping("/updateWastePrice")
    @ApiOperation(value = "更新废品价格信息")
    public Result updateWastePrice(@RequestParam("price") BigDecimal price,@RequestParam("priceId") Integer priceId, @RequestHeader String token) throws Exception {
        //获取当前登录管理员的id
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println(adminId);
        //调用修改方法
        wastePriceService.updateWastePrice(price,priceId,adminId);
        return Result.getResult().setMsg("修改废品价格成功").setCode(200);
    }

    /**
     * 删除废品价格信息
     * @param priceId 要删除的废品价格id
     * @return 返回成功信息和code
     */
    @GetMapping("/deleteWastePrice")
    @ApiOperation(value = "删除废品价格信息")
    public Result deleteWastePrice(@RequestParam("priceId") Integer priceId){
        wastePriceService.deleteWastePrice(priceId);
        return Result.getResult().setMsg("删除废品价格信息成功").setCode(200);
    }

    /**
     * 按废品价格id查询废品信息
     * @param priceId 废品价格id
     * @return 废品价格信息、成功提示和code
     */
    @GetMapping("/findWastePriceById")
    @ApiOperation(value = "按废品价格id查询废品信息")
    public Result<WastePrice> findWastePriceById(@RequestParam("priceId") Integer priceId){
        WastePrice wastePrice = wastePriceService.getById(priceId);
        return Result.getResult(WastePrice.class).setData(wastePrice).setMsg("按id查询废品价格成功").setCode(200);
    }

    /**
     * 查询所有废品价格信息
     * @param form 查询所有废品价格信息的条件
     * @return 返回成功信息和code
     */
    @PostMapping("/findAll")
    @ApiOperation(value = "查询所有废品价格信息")
    public Result<List<FindAllWastePriceDTO>> findAll(@RequestBody FindAllWastePriceForm form){
        System.out.println("类型"+form.getTypeId());
        System.out.println(form.getPageNum());
        System.out.println(form.getPageSize());
        int offset = (form.getPageNum()-1) *form.getPageSize();
        AllFindAllWastePriceDTO dto=wastePriceService.findAll(BeanUtil.toBean(form, FindAllWastePriceParam.class),offset);
        return Result.getListResult(FindAllWastePriceDTO.class).setData(dto.getList()).setMsg("查询全部废品价格信息成功").setCode(200).setTotal(dto.getTotal());
    }

    /**
     * 根据(城市/区县)查询对应废品的价格信息
     */
    @PostMapping("/findWastePriceByTypeAndCity")
    @ApiOperation(value = "根据(城市/区县)查询对应废品的价格信息")
    public Result<WastePriceDTO> findAll(@RequestBody FindWastePriceByTypeAndCityForm form){
        WastePriceDTO wastePrice=wastePriceService.findWastePriceByTypeAndCity(BeanUtil.toBean(form, FindWastePriceByTypeAndCityParam.class));
        return Result.getResult(WastePriceDTO.class).setData(wastePrice).setMsg("根据(城市/区县)查询对应废品的价格信息成功").setCode(200);
    }

    /**
     * 根据订单查询废品价格
     * */
    @GetMapping("/findByCStationId")
    @ApiOperation(value = "根据订单查询废品价格")
    public Result<List<WastePrice>> findByCStationId(@RequestParam("stationId") Integer stationId){
        StationInfo stationInfo = stationInfoController.findFatherStationById(stationId).getData();
        Integer infoStationId = stationInfo.getStationId();
        List<WastePrice> dto = wastePriceService.findByCStationId(infoStationId);
        return Result.getListResult(WastePrice.class).setData(dto).setMsg("根据订单查询废品价格").setCode(200);
    }
}

